<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Subject() {
            let listener = null
            //发布
            return {
                publish(val) {
                    if (listener) listener(val)
                },
                //订阅
                subscrible(cb) {
                    listener = cb
                }

            }

        }
        const subject = Subject()
        setTimeout(() => {
            //
            let a = 1
            const b = 1
            const c = {
                value: 1
            }
            setTimeout(() => {
                //从参数传进去
                subject.publish({ tag: 'a', value: a }) //a进去取值
                subject.publish({ tag: 'b', value: b })
                let cValue = c.value
                setTimeout(()=>{
                    cValue = 2
                    subject.publish({tag: 'c', value: {...c}})
                    console.log('发布的外面',c.value);
                })
            })

        })
    </script>
</body>

</html>